package com.contactbook.web.controller.contact;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.contactbook.web.service.ContactClient;
import com.contactbook.web.service.GroupClient;
import com.contactbook.web.util.ContactMessage;

import contactbook.model.Account;
import contactbook.model.Contact;
import contactbook.model.MaritalStatus;

public class UpdateContactController extends HttpServlet implements Servlet {

	private static final long serialVersionUID = 1L;
	
	private static final String CONFIRM_UPDATE = "OK";
	private static final String CANCEL_UPDATE = "Annuler";
	
	private GroupClient groupClient;
	private ContactClient contactClient;
	
	public UpdateContactController() {
		super();
		groupClient = new GroupClient();
		contactClient = new ContactClient();
	}
	
	public void init() throws ServletException {
    	super.init();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Index");
		dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		Account account = (Account)session.getAttribute("account");
		String submit = request.getParameter("submit");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Index");;
	
		if (account != null) {
			groupClient.login(account.getLogin(), account.getPassword());
			contactClient.login(account.getLogin(), account.getPassword());
			
			int contactId = Integer.valueOf(request.getParameter("contactId"));
			Contact contact = contactClient.getContactById(contactId);
			
			if (submit == null) {
				request.setAttribute("contact", contact);
				request.setAttribute("groups", groupClient.getGroups().iterator());
				request.setAttribute("maritalStatus", MaritalStatus.values());
				dispatcher = request.getRequestDispatcher("/updateContact.jsp");
			}
			else if (submit.equals(CONFIRM_UPDATE)) {
				ContactMessage message = updateContact(request);
				contact = contactClient.getContactById(contactId);
				request.setAttribute("message", message);
				request.setAttribute("contact", contact);
				request.setAttribute("groups", groupClient.getGroups().iterator());
				request.setAttribute("maritalStatus", MaritalStatus.values());
				dispatcher = request.getRequestDispatcher("updateContact.jsp");
			}
			else if (submit.equals(CANCEL_UPDATE)) {
    			dispatcher = request.getRequestDispatcher("/Contact");
			}
				
			contactClient.logout();
			groupClient.logout();
		}
		dispatcher.forward(request, response);
	}
	
	private ContactMessage updateContact(HttpServletRequest request) {
		int contactId = Integer.valueOf(request.getParameter("contactId"));
		Contact contact = contactClient.getContactById(contactId);
		
		if (contact != null) {
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String stringGroupId = request.getParameter("group");
			int groupId = Integer.valueOf(stringGroupId);
			String email = request.getParameter("email");
			String phoneNumber = request.getParameter("phoneNumber");
			String address = request.getParameter("address");
			String stringZipCode = request.getParameter("zipCode");
			int zipCode = (stringZipCode.equals("")) ? 0 : Integer.valueOf(stringZipCode);
			String town = request.getParameter("town");
			String country = request.getParameter("country");
			MaritalStatus maritalStatus = MaritalStatus.valueOf(request.getParameter("maritalStatus"));
			String stringAge = request.getParameter("age");
			int age = (stringAge.equals("")) ? 0 : Integer.valueOf(stringAge);
			if (lastName != null && !lastName.equals("")) {
				contact.setFirstName(firstName);
				contact.setLastName(lastName);
				if (groupId != 0)
					contact.setGroup(groupClient.getGroupById(groupId));
				else
					contact.setGroup(null);
				contact.setEmail(email);
				contact.setPhoneNumber(phoneNumber);
				contact.setAddress(address);
				if (zipCode != 0)
					contact.setZipCode(zipCode);
				contact.setTown(town);
				contact.setCountry(country);
				contact.setMaritalStatus(maritalStatus);
				if (age != 0)
					contact.setAge(age);
				contactClient.update(contact);
				return ContactMessage.CONTACT_UPDATED_OK;
			}
			else
				return ContactMessage.CONTACT_UPDATED_LASTNAME_FAIL;
		}
		return ContactMessage.CONTACT_UPDATED_FAIL;
	}
	
	public void destroy() {
		super.destroy();
	}
}
